﻿
Imports System.Threading

Namespace locks
    Public Structure monitorlock
        Implements ilock

        Private obj As Object

        Shared Sub New()
            assert([default](Of Object)() Is Nothing)
            assert([default](Of Boolean)() = False)
        End Sub

        Public Sub enter() Implements ilock.enter
            If obj Is Nothing Then
                Interlocked.CompareExchange(obj, New Object(), Nothing)
            End If
            Monitor.Enter(obj)
        End Sub

        Public Sub [exit]() Implements ilock.exit
            Monitor.Exit(obj)
        End Sub

        Public Function thread_owned() As Boolean Implements ilock.thread_owned
            Return True
        End Function
    End Structure
End Namespace
